package com.vancouver.knows.business.tracker;

import com.pangu.task.IResultReceiver;
import com.pangu.task.OperateResult;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.Constants;
import com.vancouver.knows.business.bean.CityDiscountDetailInfo;
import com.vancouver.knows.business.bean.DiscountInfo;

import java.util.List;

/**
 * 获取活动列表
 * 
 * @author chenxunlin01
 * 
 */
public class FetchDiscountDetailTracker extends ABInvokeTracker {

	public FetchDiscountDetailTracker(BusinessModule module, IResultReceiver resultReceiver) {
		super(module, resultReceiver);
	}

	@Override
	public String TAG() {
		return FetchDiscountDetailTracker.class.getSimpleName();
	}

	@Override
	public void handleResult(OperateResult result) {
		CityDiscountDetailInfo disCountDetailInfo = (CityDiscountDetailInfo) result.getResultData();
		if (disCountDetailInfo != null) {

			CityDiscountDetailInfo cachedDetailInfo = module.getCacheManager().getDiscountDetailInfoMap().get(disCountDetailInfo.getId());
			if (cachedDetailInfo != null) {
				if (cachedDetailInfo.getBusinessUserImages() != null && disCountDetailInfo.getBusinessUserImages() != null) {
					if (cachedDetailInfo.getBusinessUserImages().size() != disCountDetailInfo.getBusinessUserImages().size()) {
						imContext.handleMobEmptyMessage(Constants.BUSINESS_STATUS_CITYDISCOUNT_IMAGES_CHANGE);
					}
				}
			}

			module.getCacheManager().getDiscountDetailInfoMap().put(disCountDetailInfo.getId(), disCountDetailInfo);
			// 更新评论数
			DiscountInfo disCountInfo = module.getCacheManager().getCityDiscountInfoCache()
					.getItemInfoById(disCountDetailInfo.getId());
			if (disCountInfo != null) {
				disCountInfo.setCommentCount(disCountDetailInfo.getCommentCount());
				imContext.handleMobEmptyMessage(Constants.BUSINESS_STATUS_CITYDISCOUNT_COMMENT_COUNT_CHANGE);
			}

			List<DiscountInfo> mapDiscountList = module.getCacheManager().getMapDiscountList();
			if(mapDiscountList!=null){
				for (DiscountInfo discountInfo:mapDiscountList) {
					if(disCountDetailInfo.getId().equals(discountInfo.getId())){
						discountInfo.setCommentCount(disCountDetailInfo.getCommentCount());
						imContext.handleMobEmptyMessage(Constants.BUSINESS_STATUS_CITYDISCOUNT_MAP_COMMENT_COUNT_CHANGE);
						break;
					}
				}
			}
		}
	}
}
